/*
 Copyright (c) 2024 Broin All rights reserved.
 Use of this source code is governed by a BSD-style
 license that can be found in the LICENSE file.
 */

// This must be first
#include "vmlinux.h"

// These must be below vmlinux.h
#include <bpf/bpf_core_read.h> /* for BPF CO-RE helpers */
#include <bpf/bpf_helpers.h>   /* most used helpers: SEC, __always_inline, etc */
#include <bpf/bpf_tracing.h>   /* for getting kprobe arguments */

#ifndef PAGE_SIZE
#define PAGE_SIZE 4096
#endif // !PAGE_SIZE

#ifndef MAPS__H
#define MAPS__H

/**
 * @description: RINGBUF宏
 * @param NAME: RINGBUF名字
 * @param PAGES: 页大小，4096 Bytes一页
 * @param PIN: 如需持久化存储，PIN到某一文件系统中
 */
#define BPF_RINGBUF(NAME, PAGES, PIN)                    \
    struct                                               \
    {                                                    \
        __uint(type, BPF_MAP_TYPE_RINGBUF);              \
        __uint(max_entries, ((1 << PAGES) * PAGE_SIZE)); \
        __uint(pinning, PIN);                            \
    } NAME SEC(".maps")

/**
 * @description: HASH_MAP宏
 * @param NAME: HASH_MAP名字
 * @param KEY: key
 * @param VALUE: value
 * @param FLAGS: 分配类型 FLAGS
 */
#define BPF_HASH(NAME, KEY, VALUE, SIZE, PIN, FLAGS) \
    struct                                           \
    {                                                \
        __uint(type, BPF_MAP_TYPE_HASH);             \
        __uint(max_entries, SIZE);                   \
        __type(key, KEY);                            \
        __type(value, VALUE);                        \
        __uint(map_flags, FLAGS);                    \
        __uint(pinning, PIN);                        \
    } NAME SEC(".maps")

#endif // # MAPS__H